#include "protocolcodeing.h"

ProtocolCodeing::ProtocolCodeing()
{

}

int ProtocolCodeing::parseBasicInformation(QByteArray codeStr, FleetAgvBasicInfo &infoOut)
{
    QString tmp_frame=codeStr;
    //截取有效字符
    //开头处理
    QString startFlag = "ST#";
    int startIndex=tmp_frame.indexOf(startFlag);
    if(0>startIndex)
    {
        return -1;
    }
    tmp_frame=tmp_frame.mid(startIndex+3);
    //末尾处理
    startFlag = '\r';
    startIndex=tmp_frame.indexOf(startFlag);
    if(0>startIndex)
    {
        return -2;
    }
    tmp_frame=tmp_frame.mid(0,startIndex);

    QString tmpStrVal;
    //提取status
    tmpStrVal=tmp_frame.section(";",0,0);
    infoOut.state=(E_AGV_STATE)tmpStrVal.toInt();
    //提取x
    tmpStrVal=tmp_frame.section(";",1,1);
    infoOut.position.x=tmpStrVal.toDouble();
    //提取y
    tmpStrVal=tmp_frame.section(";",2,2);
    infoOut.position.y=tmpStrVal.toDouble();
    //提取r
    tmpStrVal=tmp_frame.section(";",3,3);
    infoOut.position.rotate=tmpStrVal.toDouble();
    //提取vbattery
    tmpStrVal=tmp_frame.section(";",4,4);
    infoOut.batteryLeftLightModeRatio=tmpStrVal.toDouble();
    //提取lineSpeed
    tmpStrVal=tmp_frame.section(";",5,5);
    infoOut.lineSpeed=tmpStrVal.toDouble();
    //提取angleSpeed
    tmpStrVal=tmp_frame.section(";",6,6);
    infoOut.rotateSpeed=tmpStrVal.toDouble();
    //提取isLoaded
    tmpStrVal=tmp_frame.section(";",7,7);
    infoOut.isLoaded=tmpStrVal.toInt();
    //errorId
    tmpStrVal=tmp_frame.section(";",8,8);
    infoOut.errorId=tmpStrVal.toInt();
    //warnId
    tmpStrVal=tmp_frame.section(";",9,9);
    infoOut.warnId=tmpStrVal.toInt();
    //noticeId
    tmpStrVal=tmp_frame.section(";",10,10);
    infoOut.noticeId=tmpStrVal.toInt();
    //heartBeat
    tmpStrVal=tmp_frame.section(";",11,11);
    infoOut.heartBeat=tmpStrVal.toInt();
    //targetPositionLeft_line
    tmpStrVal=tmp_frame.section(";",12,12);
    infoOut.targetPositionLeft_line=tmpStrVal.toDouble();
    //targetPositionLeft_angle
    tmpStrVal=tmp_frame.section(";",13,13);
    infoOut.targetPositionLeft_angle=tmpStrVal.toDouble();

    return 1;
}

int ProtocolCodeing::parseMessageInformation(QByteArray codeStr, QString &msgOut)
{
//    QString tmp_frame=QString::fromLatin1(codeStr);
    QString tmp_frame=QString::fromUtf8(codeStr);
    //截取有效字符
    //开头处理
    QString startFlag = "MSGINFO#";
    int startIndex=tmp_frame.indexOf(startFlag);
    if(0>startIndex)
    {
        return -1;
    }
    tmp_frame=tmp_frame.mid(startIndex+8);
    //末尾处理
    startFlag = '\r';
    startIndex=tmp_frame.indexOf(startFlag);
    if(0>startIndex)
    {
        return -2;
    }
    msgOut=tmp_frame.mid(0,startIndex);


    return 1;
}

int ProtocolCodeing::isCommandReplyOk(QByteArray codeStr)
{
    if(0==codeStr.size())
    {
        return 0;
    }
    QString replyStr=codeStr;
    QString okStr="\nOK\r";
    QString errorStr="\nBD\r";
    int index=replyStr.indexOf(errorStr);
    if(0<=index)
    {
        return -1;
    }
    index=replyStr.indexOf(okStr);
    if(0<=index)
    {
        return 1;
    }
    return 0;
}
